package com.ledim.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.FanDataBean;
import com.ledim.bean.FocusDataBean;
import com.ledim.bean.base.BaseResultDataInfo;
import com.ledim.ledimview.LedimUserIconView;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import com.letv.android.young.client.R;

/**
 * Created by ccx on 16/8/17.
 * 我的关注和粉丝列表
 */
public class LedimFollowUsersAdapter extends LedimBaseListAdapter<FanDataBean> {

    private ViewHolder holder;

    public LedimFollowUsersAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_follow_fans, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ImageView iv_follow_state =
                (ImageView) convertView.findViewById(R.id.iv_follow_state);
        final FanDataBean dataBean = getItem(position);
        //设置状态信息
        iv_follow_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFollowAction(dataBean, iv_follow_state);
            }
        });

        if (dataBean.user != null) {
            if (LedimSpUtils.isMe(dataBean.user.userId)) {
                //是本人
                iv_follow_state.setVisibility(View.GONE);
            } else {
                iv_follow_state.setVisibility(View.VISIBLE);
            }

            setFocusBgByState(dataBean.type, iv_follow_state);
            holder.tv_coin_counts.setText(dataBean.user.coin);
            holder.tv_user_name.setText(dataBean.user.nickname);
            holder.iv_user_icon.setFollowUserIcon(dataBean.user.userId, dataBean.user.status, dataBean.user.avatar.thumb);
        }
        return convertView;
    }

    public static class ViewHolder {

        private LedimUserIconView iv_user_icon;
        private TextView tv_user_name, tv_coin_counts;

        public ViewHolder(View convertView) {
            iv_user_icon = (LedimUserIconView) convertView.findViewById(R.id.iv_user_icon);
            tv_user_name = (TextView) convertView.findViewById(R.id.tv_user_name);
            tv_coin_counts = (TextView) convertView.findViewById(R.id.tv_coin_counts);
        }
    }

    /**
     * 根据状态刷新背景
     */
    private void setFocusBgByState(int focusState, ImageView imageView) {
        switch (focusState) {
            case ConstantUtil.NOT_FOCUS_STATE:
                imageView.setImageResource(R.drawable.fan_not_focus);
                break;
            case ConstantUtil.ALREADY_FOCUS_STATE:
                imageView.setImageResource(R.drawable.fan_already_focus);
                break;
            case ConstantUtil.TOGETHER_FOCUS_STATE:
                imageView.setImageResource(R.drawable.fan_together_focus);
                break;
        }
    }

    public void doFollowAction(final FanDataBean dataBean, final ImageView imageView) {
        LedimHttpObserver observer = new LedimHttpObserver<BaseResultDataInfo<FocusDataBean>>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onNext(BaseResultDataInfo<FocusDataBean> data) {
                super.onNext(data);
                if (data != null) {
                    if (data.success) {
                        //成功
                        dataBean.type = data.data.type;
                        setFocusBgByState(data.data.type, imageView);
                    } else {
                        //失败
                        LedimApiServiceUtils.followFailMsg(dataBean.type);
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                LogUtil.e(e.getMessage());
                LedimApiServiceUtils.followFailMsg(dataBean.type);
            }
        };

        switch (dataBean.type) {
            case ConstantUtil.NOT_FOCUS_STATE:
                LedimApiServiceUtils.requestFanAndFollowData(ConstantUtil.NOT_FOCUS_STATE, dataBean.user.userId, observer);
                break;
            case ConstantUtil.ALREADY_FOCUS_STATE:
            case ConstantUtil.TOGETHER_FOCUS_STATE:
                LedimApiServiceUtils.requestFanAndFollowData(ConstantUtil.ALREADY_FOCUS_STATE, dataBean.user.userId, observer);
                break;
        }

    }
}
